package com.spider.dmeo8test;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class MovieService {
    //4.定义一个集合容器，存储全部上架的电影数据。
    private  static List<Movie> movies =new ArrayList<Movie>();
    private static Scanner sc = new Scanner(System.in);
    //给movies里面添加一些初始数据
   static {
    // 添加中国武侠电影
    movies.add(new Movie("卧虎藏龙",8.5,"李安",29.9));
    movies.add(new Movie("英雄",8.2,"张艺谋",25.0));
    movies.add(new Movie("十面埋伏",7.8,"张艺谋",22.5));
    movies.add(new Movie("功夫",8.0,"周星驰",18.0));
    movies.add(new Movie("叶问",8.3,"叶伟信",20.0));
}

    public void start() {
        while (true) {
            //3.准备操作界面,cmd界面
            System.out.println("====电影信息操作系统====");
            System.out.println("1.上架");
            System.out.println("2.下架某个电影");
            System.out.println("3.查询某个电影");
            System.out.println("4.封杀某个明星");
            System.out.println("5.退出");
            System.out.println("请您输入操作命令：");
            int command = sc.nextInt();
            switch (command) {
                case 1:
                    addMovie();
                    break;
                case 2:
                    removeMovie();
                    break;
                case 3:
                    queryMovie();
                    break;
                case 4:
                    killMovie();
                    break;
                    case 5:
                    System.out.println("感谢使用！");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入的命令有误！");
            }
        }
    }

    private void killMovie() {
    System.out.println("========封杀明星========");
    System.out.println("请输入明星名称：");
    String actor = sc.next();
    List<Movie> moviesToRemove = queryMoviesByActor(actor);
    // 如果找到了该明星的电影
    if (!moviesToRemove.isEmpty()) {
        movies.removeAll(moviesToRemove);
        System.out.println(actor + "封杀成功！共删除" + moviesToRemove.size() + "部电影！");
    } else {
        System.out.println("没有此明星！");
    }
}



    private void removeMovie() {
        System.out.println("========下架电影========");
        System.out.println("请输入电影名称：");
        String name = sc.next();
        Movie movie = queryMovieByName(name);
        if (movie != null) {
            movies.remove(movie);
            System.out.println("下架成功！");
        }
        else {
            System.out.println("没有此电影！无需下架");
        }
    }

    private void queryMovie() {
        System.out.println("========查询电影========");
        System.out.println("请输入电影名称：");
        String name = sc.next();
        Movie movie = queryMovieByName(name);
        if (movie != null) {
            System.out.println(movie);
        }
        else {
            System.out.println("没有此电影！");
        }
    }
    //创建一个方法通过名称查询电影
    public Movie queryMovieByName(String name) {
       for (Movie movie : movies) {
           if (movie.getName().equals(name)) {
               return movie;
           }
       }
       return null;
    }
    private List<Movie> queryMoviesByActor(String actor) {
    List<Movie> result = new ArrayList<>();
    for (Movie movie : movies) {
        if (movie.getActor().equals(actor)) {
            result.add(movie);
        }
    }
    return result;
}


    private void addMovie() {
       System.out.println("========添加电影========");
        System.out.println("请输入电影名称：");
        String name = sc.next();
        System.out.println("请输入电影评分：");
        double score = sc.nextDouble();
        System.out.println("请输入电影主演：");
        String actor = sc.next();
        System.out.println("请输入电影价格：");
        double price = sc.nextDouble();
        Movie movie = new Movie( name, score, actor, price);
        System.out.println("添加电影成功！");
        movies.add(movie);
    }
}
